<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/laypage.css">
    <script src="js/vue/vue.min.js"></script>
    <script src="js/jquery/jquery.min.js"></script>
    <script src="js/laypage/laypage.js" charset="utf-8"></script>
    <script src="js/layer/layer.js" charset="utf-8"></script>
    <script src="js/jquery.validation/1.14.0/jquery.validate.min.js"></script>
    <script src="js/jquery.validation/1.14.0/messages_zh.min.js"></script>
</head>
<body>
<style>
    input.error {
        border: 1px solid #E6594E;
    }
</style>

<div class="container">

    <form id="updatePersonForm">
        <br/>
        <div class="form-group">
            <label for="name">用户名称：</label>
            <input type="text" class="form-control" id="name"  value="${person.name}" placeholder="输入名称">
        </div>
        <div class="form-group">
            <label for="password">用户密码：</label>
            <input type="text" class="form-control" id="password" value="${person.password}"
                   placeholder="请设置密码">
        </div>
        <div class="form-group">
            <label for="age">用户年龄：</label>
            <input type="text" class="form-control" id="age" value="${person.age}" placeholder="输入年龄">
        </div>
        <div class="form-group">
            <button type="button" id="updateBtn" class="btn btn-success">修改</button>
            <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
        </div>
    </form>
</div>

<script>
    var updatePerson = function () {
        if (!check().form()) {
            return;
        }
        $.ajax({
            type: 'POST',
            data: {
                id:${person.id},
                name: $("#name").val(),
                password: $("#password").val(),
                age: $("#age").val()
            },
            url: 'person_update',
            dataType: 'json',
            success: function () {
                $("#cancelBtn").click();
            }
        })
    };

    $("#updateBtn").click(function () {
        updatePerson();
    });



    $("#cancelBtn").click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });


    function check() {
        return $("#updatePersonForm").validate({
            rules: {
                name: {
                    required: true
                },
                password: {
                    required: true
                },
                age: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: ""
                },
                password: {
                    required: ""
                },
                age: {
                    required: ""
                }
            }
        })
    }


</script>
</body>
</html>
